package com.ruoyi.bantong.service.impl;

import java.math.BigDecimal;
import java.security.Security;
import java.util.List;

import com.ruoyi.bantong.domain.BtUserLevel;
import com.ruoyi.bantong.mapper.BtUserMapper;
import com.ruoyi.bantong.service.IBtUserAccountRecordService;
import com.ruoyi.common.core.domain.entity.BtUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtUserScoreRecordMapper;
import com.ruoyi.bantong.domain.BtUserScoreRecord;
import com.ruoyi.bantong.service.IBtUserScoreRecordService;

/**
 * 用户版通分兑换记录Service业务层处理
 * 
 * @author laohei
 * @date 2021-02-20
 */
@Service
public class BtUserScoreRecordServiceImpl implements IBtUserScoreRecordService 
{
    @Autowired
    private BtUserScoreRecordMapper btUserScoreRecordMapper;
    @Autowired
    private IBtUserAccountRecordService accountRecordService;
    @Autowired
    private BtUserMapper btUserMapper;

    /**
     * 查询用户版通分兑换记录
     * 
     * @param ucrId 用户版通分兑换记录ID
     * @return 用户版通分兑换记录
     */
    @Override
    public BtUserScoreRecord selectBtUserScoreRecordById(Integer ucrId)
    {
        return btUserScoreRecordMapper.selectBtUserScoreRecordById(ucrId);
    }

    /**
     * 查询用户版通分兑换记录列表
     * 
     * @param btUserScoreRecord 用户版通分兑换记录
     * @return 用户版通分兑换记录
     */
    @Override
    public List<BtUserScoreRecord> selectBtUserScoreRecordList(BtUserScoreRecord btUserScoreRecord)
    {
        return btUserScoreRecordMapper.selectBtUserScoreRecordList(btUserScoreRecord);
    }

    /**
     * 新增用户版通分兑换记录
     * 
     * @param btUserScoreRecord 用户版通分兑换记录
     * @return 结果
     */
    @Override
    public int insertBtUserScoreRecord(BtUserScoreRecord btUserScoreRecord)
    {
        //判断余额
        BtUser btUser = btUserMapper.selectBtUserById(btUserScoreRecord.getUserId());
        BigDecimal btScore = btUser.getBtScore();//积分余额
        BigDecimal score = btUserScoreRecord.getScore();//扣除积分
        if(score.compareTo(btScore) == 1){
            throw new CustomException("用户积分不足！");
        }
        int i = btUserScoreRecordMapper.insertBtUserScoreRecord(btUserScoreRecord);
        //账户变动
        accountRecordService.updataUserAccount(btUserScoreRecord.getUserId(),BigDecimal.ZERO,btUserScoreRecord.getScore(),
                btUserScoreRecord.getOsid(),"bt_user_score_record",
                "out",15);
        btUserScoreRecord.setCreateBy(SecurityUtils.getUsername());
        btUserScoreRecord.setCreateTime(DateUtils.getNowDate());
        return i;
    }

    /**
     * 修改用户版通分兑换记录
     * 
     * @param btUserScoreRecord 用户版通分兑换记录
     * @return 结果
     */
    @Override
    public int updateBtUserScoreRecord(BtUserScoreRecord btUserScoreRecord)
    {
        btUserScoreRecord.setUpdateTime(DateUtils.getNowDate());
        return btUserScoreRecordMapper.updateBtUserScoreRecord(btUserScoreRecord);
    }

    /**
     * 批量删除用户版通分兑换记录
     * 
     * @param ucrIds 需要删除的用户版通分兑换记录ID
     * @return 结果
     */
    @Override
    public int deleteBtUserScoreRecordByIds(Integer[] ucrIds)
    {
        return btUserScoreRecordMapper.deleteBtUserScoreRecordByIds(ucrIds);
    }

    /**
     * 删除用户版通分兑换记录信息
     * 
     * @param ucrId 用户版通分兑换记录ID
     * @return 结果
     */
    @Override
    public int deleteBtUserScoreRecordById(Integer ucrId)
    {
        return btUserScoreRecordMapper.deleteBtUserScoreRecordById(ucrId);
    }
}
